package com.hoperun.controller.admin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresUser;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



import com.hoperun.bean.AdminDictionaryType;
import com.hoperun.controller.common.BaseController;
import com.hoperun.framework.ex.XHZException;
import com.hoperun.framework.ibatis.page.BootGrid;
import com.hoperun.framework.ibatis.page.BootGridPage;
import com.hoperun.framework.models.DataView;
import com.hoperun.framework.query.Criteria;
import com.hoperun.framework.util.Constant;
import com.hoperun.service.IAdminDictionaryTypeSV;
import com.hoperun.utils.ErrorCode;


/**
 * Copyright: Copyright (c) 2016 GUO~AN <br/>
 * ClassName: AdminDictionaryTypeController.java
 * 
 * @Description: 
 * 
 * @version: v1.0.0
 * @author: fuyu
 * @date: 2016-1-8 上午9:11:06
 * 
 *        Modification History: Date Author Version Description
 *        ---------------------------------------------------------* 2016-1-8
 *        fuyu v1.0.0  修改理由
 */
@Controller
@RequestMapping("/admin/admin/dictionary/type")
@RequiresUser
public class AdminDictionaryTypeController  extends BaseController 
{
    
	@Resource
	private IAdminDictionaryTypeSV adminDictionaryTypeSV;
	
	@ModelAttribute
	public void populateModel(final HttpServletRequest request ,ModelMap modelMap)
	{
        try {
    		super.initMenus(request, modelMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/{menu}/{menuNext}")
	public String index(@PathVariable long menu, @PathVariable long menuNext,
    ModelMap modelMap) 
    {

    	modelMap.put("menuType", menu);
		modelMap.put("menuNextType", menuNext);
        
		return "admin:admindictionarytype.list";
	}
	
	@RequestMapping("/page")
	@ResponseBody
	public BootGridPage<AdminDictionaryType> identityUserPage(BootGrid<AdminDictionaryType> grid,
			AdminDictionaryType adminDictionaryType, ModelMap modelMap) {
		BootGridPage<AdminDictionaryType> pageModel = new BootGridPage<AdminDictionaryType>();

		try {
			Criteria criteria = new Criteria();

			if (null == grid)
				return new BootGridPage<AdminDictionaryType>();


			pageModel = adminDictionaryTypeSV.queryPage(criteria, grid);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return pageModel;
	}	
	
	@RequestMapping("/create")
	public String create(ModelMap modelMap)
	{
		modelMap.addAttribute("method", "save");
		return "adminAlone:admindictionarytype.info";
	}
	
	@RequestMapping("/modify/{id}")
	public String modify(@PathVariable
	Long id, ModelMap modelMap, final HttpServletRequest request)
	{
		AdminDictionaryType adminDictionaryType = null;
		try
		{
			adminDictionaryType = adminDictionaryTypeSV.queryById(id);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		modelMap.addAttribute("method", "update");
		modelMap.addAttribute("row", adminDictionaryType);
		return "adminAlone:admindictionarytype.info";
	}
	
	@RequestMapping("/detail/{id}")
	public String detail(@PathVariable
	Long id, ModelMap modelMap, final HttpServletRequest request)
	{
		AdminDictionaryType adminDictionaryType = null;
		try
		{
			adminDictionaryType = adminDictionaryTypeSV.queryById(id);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		modelMap.addAttribute("detail", adminDictionaryType);
		return "adminAlone:admindictionarytype.detail";
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public DataView save(AdminDictionaryType adminDictionaryType)
	{
		DataView dataView = new DataView();
		
		try
		{
			adminDictionaryTypeSV.save(adminDictionaryType);
			
			dataView.setCode(Constant.SUCCESS);
			dataView.setMessage("新增成功");
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return dataView;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public DataView update(AdminDictionaryType adminDictionaryType)
	{
		DataView dataView = new DataView();
		
		if (null == adminDictionaryType || StringUtils.isBlank(""+ adminDictionaryType.getDtypeId()))
		{
			throw new XHZException(ErrorCode.MISS_PARAM, null,
					new Object[] { "id信息" });
		}
		
		try
		{
			adminDictionaryTypeSV.update(adminDictionaryType);
			
			dataView.setCode(Constant.SUCCESS);
			dataView.setMessage("修改成功");
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return dataView;
	}
	
	@RequestMapping("/find/{id}")
	@ResponseBody
	public AdminDictionaryType findOne(@PathVariable
	Long id)
	{
		AdminDictionaryType adminDictionaryType = null;
		try
		{
			adminDictionaryType = adminDictionaryTypeSV.queryById(id);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		return adminDictionaryType;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public DataView delete(Long[] ids)
	{
		DataView dataView = new DataView();
		AdminDictionaryType adminDictionaryType = null;
		try
		{
			if (ArrayUtils.isEmpty(ids))
			{
				throw new XHZException(ErrorCode.ILLEGAL_PARAM, null,
						new Object[] { "id" });
			}
			for (Long id : ids)
			{
				adminDictionaryType = new AdminDictionaryType();
				adminDictionaryType.setDtypeId(id);
			//	adminDictionaryTypeSV.update(adminDictionaryType);
				
			}
			dataView.setCode(Constant.SUCCESS);
			dataView.setMessage("操作成功");
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		return dataView;
	}
}
